package com.centling.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.centling.business.CustomerBean;
import com.centling.business.OfflinemeasureVO;
import com.centling.service.ICustomerService;
import com.centling.service.IOfflinemeasureService;
import com.centling.util.Encryption;
import com.centling.util.LocalUtils;

@Controller
@Transactional
public class OfflinemeasureController {
	
	private static Logger log = Logger.getLogger(OfflinemeasureController.class);
	
	@Resource
	private IOfflinemeasureService offlinemeasureService;
	
	@RequestMapping("/offlinemeasureImportCRM.do")
	@ResponseBody
	@Transactional
	public String ecOfflinemeasureImport(HttpServletRequest request,HttpServletResponse response,
			@RequestParam String encryptionJsonStr) {	
		log.info("function ecOfflinemeasureImport start..., with param encryptionJsonStr:"+encryptionJsonStr);
		Boolean returnVal = false;
		try {
			String jsonStr=Encryption.decrypt(encryptionJsonStr, CustomerController.RC_CRM_KEY); 
			log.info("param after: " + jsonStr);
			JSONArray jsonArr = JSON.parseArray(jsonStr);
			List<OfflinemeasureVO> list = new ArrayList<OfflinemeasureVO>();
			for (int i=0; i<jsonArr.size(); i++){
				OfflinemeasureVO vo = JSON.toJavaObject(jsonArr.getJSONObject(i), OfflinemeasureVO.class);
				list.add(vo);
			}
			returnVal = offlinemeasureService.save(list);
			log.info("function ecOfflinemeasureImport end..., with return " + returnVal);
			return returnVal.toString();
		} catch (Exception e) {
			log.info("function ecOfflinemeasureImport end..., with return " + returnVal);
			log.error("function ecOfflinemeasureImport exception..., with error: " + e.getMessage());
			e.printStackTrace();
			return returnVal.toString();
		}
	}
}
